package org.example.exampreparationtreasure.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;

import java.util.List;

@Mapper
public interface RecommendationMapper {

    /**
     * 获取用户的薄弱分类ID列表（简化版 - 单表查询）
     * @param userId 用户ID
     * @return 薄弱分类ID列表
     */
    @Select("SELECT DISTINCT category_id " +
            "FROM incorrect_question_records " +
            "WHERE user_id = #{userId} " +
            "ORDER BY mistake_count DESC " +
            "LIMIT 5")
    List<Long> selectWeakCategoryIdsByUserId(@Param("userId") Long userId);

    /**
     * 获取用户答题统计（简化版 - 单表查询）
     * @param userId 用户ID
     * @return 答题统计
     */
    @Select("SELECT " +
            "    COUNT(*) as totalAnswers, " +
            "    SUM(CASE WHEN is_correct = 1 THEN 1 ELSE 0 END) as correctAnswers " +
            "FROM answer_records " +
            "WHERE user_id = #{userId}")
    @Results({
            @Result(property = "totalAnswers", column = "totalAnswers"),
            @Result(property = "correctAnswers", column = "correctAnswers")
    })
    UserAnswerStats selectUserAnswerStats(@Param("userId") Long userId);

    /**
     * 根据分类ID列表获取推荐题目（简化版）
     * @param categoryIds 分类ID列表
     * @param size 题目数量
     * @return 题目ID列表
     */
    @Select("<script>" +
            "SELECT question_id FROM questions " +
            "WHERE 1=1 " +
            "<if test='categoryIds != null and categoryIds.size() > 0'>" +
            "   AND category_id IN " +
            "   <foreach collection='categoryIds' item='categoryId' open='(' separator=',' close=')'>" +
            "       #{categoryId}" +
            "   </foreach>" +
            "</if>" +
            "ORDER BY RAND() " +
            "LIMIT #{size}" +
            "</script>")
    List<Long> selectRecommendedQuestionIds(
            @Param("categoryIds") List<Long> categoryIds,
            @Param("size") Integer size);

    /**
     * 随机获取题目ID（最简化版本）
     * @param size 题目数量
     * @return 题目ID列表
     */
    @Select("SELECT question_id FROM questions " +
            "ORDER BY RAND() " +
            "LIMIT #{size}")
    List<Long> selectRandomQuestionIds(@Param("size") Integer size);

    /**
     * 根据分类ID列表和难度获取推荐题目
     * @param categoryIds 分类ID列表
     * @param difficulty 难度级别（中文格式）
     * @param size 题目数量
     * @return 题目ID列表
     */
    @Select("<script>" +
            "SELECT question_id FROM questions " +
            "WHERE 1=1 " +
            "<if test='categoryIds != null and categoryIds.size() > 0'>" +
            "   AND category_id IN " +
            "   <foreach collection='categoryIds' item='categoryId' open='(' separator=',' close=')'>" +
            "       #{categoryId}" +
            "   </foreach>" +
            "</if>" +
            "<if test='difficulty != null and difficulty != \"\"'>" +
            "   AND difficulty = #{difficulty}" +
            "</if>" +
            "ORDER BY RAND() " +
            "LIMIT #{size}" +
            "</script>")
    List<Long> selectRecommendedQuestionIdsWithDifficulty(
            @Param("categoryIds") List<Long> categoryIds,
            @Param("difficulty") String difficulty,
            @Param("size") Integer size);

    // 统计类 - 用户答题统计
    class UserAnswerStats {
        private Integer totalAnswers;
        private Integer correctAnswers;

        // getter和setter
        public Integer getTotalAnswers() { return totalAnswers; }
        public void setTotalAnswers(Integer totalAnswers) { this.totalAnswers = totalAnswers; }
        public Integer getCorrectAnswers() { return correctAnswers; }
        public void setCorrectAnswers(Integer correctAnswers) { this.correctAnswers = correctAnswers; }

        /**
         * 计算正确率
         */
        public Double getAccuracyRate() {
            if (totalAnswers == null || totalAnswers == 0) {
                return 0.0;
            }
            return (correctAnswers * 100.0) / totalAnswers;
        }
    }
}